/**
 * @author davelenz
 */
package ypmits.data.remoting {
	import flash.events.NetStatusEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.Responder;

	public class Remoting extends EventDispatcher {

		private var rs : RemotingService;
		
		public var classpath : String;
		public var data : *;

		
		function Remoting( _gateway : String, _classpath : String, _arg : * ) {
			classpath = _classpath;
			
			rs = new RemotingService( _gateway );
			rs.addEventListener( NetStatusEvent.NET_STATUS, netStatusEventHandler );
			var responder : Responder = new Responder( onResult, onFault );
			rs.call( _classpath, responder, _arg );
		}

		
		private function netStatusEventHandler( e : NetStatusEvent ) : void {
			trace( "Remoting.netStatusEventHandler("+[e.info['code'],e.info['level']]+")" );
		}

		
		private function onResult(re : *) : void {
			data = re;
			dispatchEvent( new Event( Event.COMPLETE ) );
		}

		
		private function onFault(fault : *) : void {
			for each (var i : * in fault) {
				trace(i);
			}
			dispatchEvent( new IOErrorEvent( IOErrorEvent.IO_ERROR ) );
		}
	}
}